package net.aiion.weave.impl.naming;

import java.lang.annotation.Annotation;
import java.util.Arrays;

import net.aiion.weave.spi.naming.Context;
import net.aiion.weave.spi.naming.Contextual;
import net.aiion.weave.spi.naming.ContextualException;
import net.aiion.weave.spi.naming.annotations.NotScoped;

public class ContextualValue implements Contextual
{
    private final String[] contextualNames;
    private Object contextualValue;

    public ContextualValue(final String aContextualName, final Object aContextualValue) {
        this(new String[] {
            aContextualName
        }, aContextualValue);
    }

    public ContextualValue(final String[] aContextualNames, final Object aContextualValue) {
        if (aContextualNames == null || aContextualValue == null) {
            throw new ContextualException("Both binding name and value must not be null");
        }

        contextualNames = Arrays.copyOf(aContextualNames, aContextualNames.length);
        contextualValue = aContextualValue;
    }

    @Override
    public final String[] getNames() {
        return contextualNames;
    }

    @Override
    public final Object getValue() {
        return contextualValue;
    }

    @Override
    public Class<? extends Annotation> getScope() {
        return NotScoped.class;
    }

    @Override
    public void onBound(final Context aContext) {
    }

    @Override
    public void onUnbound(final Context aContext) {
    }

    @Override
    public String toString() {
        final StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("ContextualValue [");
        stringBuilder.append(Arrays.toString(contextualNames));
        stringBuilder.append(", ");
        stringBuilder.append(contextualValue);
        stringBuilder.append("]");
        return stringBuilder.toString();
    }
}
